//
//  QuestionDetailViewController.m
//  Findif
//
//  Created by Mac on 2019/1/11.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "QuestionDetailViewController.h"

@interface QuestionDetailViewController ()

@property (nonatomic, weak)UILabel * questionLabel;
@property (nonatomic, weak)UILabel * ansewrLabel;
@property (nonatomic, weak)UIWebView * webView;

@end

@implementation QuestionDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"问题详情";
    self.view.backgroundColor = ZRWhiteColor;
    
    [self setupUI];
}

- (void)setupUI{
    
    UILabel * questionLabel = [UILabel new];
    
    questionLabel.text = self.question;
    questionLabel.textColor = ZRBlackColor;
    questionLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    questionLabel.numberOfLines = 0;
    
    self.questionLabel = questionLabel;
    
    [self.view addSubview:self.questionLabel];
    
//    UIWebView * webView = [UIWebView new];
//
//    webView.backgroundColor = ZRWhiteColor;
////    NSURL *url = [[NSBundle mainBundle] urlForResource:@"localHtml.html" withExtension:nil];
//    [webView loadHTMLString:string.copy baseURL:nil];
////    NSLog(@"url===%@",url);
//    self.webView = webView;
//
//    [self.view addSubview:self.webView];
    //1.将字符串转化为标准HTML字符串
    NSString * str = [self htmlEntityDecode:self.answer];
    //2.将HTML字符串转换为attributeString
    NSAttributedString * attributeStr = [self attributedStringWithHTMLString:str];
    
//    //3.使用label加载html字符串
//    self.label.attributedText = attributeStr;
    
    UILabel * ansewrLabel = [UILabel new];
    
    ansewrLabel.text = self.question;
    ansewrLabel.textColor = ZRBlackColor;
    ansewrLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    ansewrLabel.numberOfLines = 0;
    ansewrLabel.attributedText = attributeStr;
    
    self.ansewrLabel = ansewrLabel;
    
    [self.view addSubview:self.ansewrLabel];
}


- (void)viewDidLayoutSubviews{
    
    [self.questionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(topHeight + 10 * SCREENH6);
    }];
    
//    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.mas_equalTo(self.questionLabel);
//        make.top.mas_equalTo(self.questionLabel.mas_bottom).offset(10 * SCREENH6);
//        make.bottom.mas_equalTo(self.view);
//    }];
    
    [self.ansewrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.questionLabel);
        make.top.mas_equalTo(self.questionLabel.mas_bottom).offset(10 * SCREENH6);
//        make.bottom.mas_equalTo(self.view);
    }];
}


//将 &lt 等类似的字符转化为HTML中的“<”等
- (NSString *)htmlEntityDecode:(NSString *)string
{
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"
    
    return string;
}

//将HTML字符串转化为NSAttributedString富文本字符串
- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString
{
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                               NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    
    return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
}

//去掉 HTML 字符串中的标签
- (NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}



@end
